package com.googlecode.jbeansdb.impl;

import org.apache.log4j.Logger;

import com.googlecode.jbeansdb.StorageEngine;

import tokyocabinet.HDB;


public class TcStorageEngine implements StorageEngine {
	private static final Logger log = Logger.getLogger(TcStorageEngine.class);
	private HDB db = new HDB();
	
	public TcStorageEngine() {
        if(db.open("/tmp/hdb.db", HDB.OWRITER | HDB.OCREAT)) {
        	log.info("open successful");
        } else {
            log.error("open fail");
        }
	}
	
	public boolean put(String key, String value) {
        boolean ret = db.put(key, value);
        if(!ret) {
        	log.error("put error:" + HDB.errmsg(db.ecode()));
        }
        
        return ret;
	}
	
	public Object get(String key) {
        return db.get(key);
	}
	
	public boolean delete(String key) {
		return this.db.out(key);
	}
	
	public void close() {
		db.close();
	}
}
